import { storeBindingsBehavior } from "mobx-miniprogram-bindings";
import mall from "@src/store/mall";
import location from "@src/store/location";
import router from "@src/router";

Component({
  behaviors: [storeBindingsBehavior],
  storeBindings: [{
    store: mall,
    fields: ['curLocation'],
    actions: ['initLocation', 'checkAuth']
  }, {
    store: location,
    actions: ['setAuth']
  }],
  properties: {
    goodsDelivery: {
      type: Object,
      value: {},
      observer: "initDelivery"
    },
    presell: {
      type: Boolean,
      value: false,
    },
    goodsPresell: {
      type: Object
    }
  },
  data: {
    express: null,
    local: null,
  },
  methods: {
    initDelivery(goodsDelivery) {
      if (!goodsDelivery || goodsDelivery.length === 0) {
        this.setData({ local: null, express: null });
        return;
      }
      let local = null;
      let express = null;

      for (let i = 0; i < goodsDelivery.length; i++) {
        if (goodsDelivery[i].deliveryType === 'EXPRESS') {
          express = goodsDelivery[i];
        }
        if (goodsDelivery[i].deliveryType === 'LOCAL') {
          local = goodsDelivery[i];
        }
      }
      this.setData({ local, express });
    },
    handleToAddress() {
      router.push({ name: "mall/address" })
    }
  }
});
